package com.puer.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.puer.common.Code;
import com.puer.common.Result;
import com.puer.domain.User;
import com.puer.service.IUserService;
import com.puer.utils.Jwt;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChadYang
 * @since 2025-10-31
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户接口")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private Jwt jwt;
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result login(@RequestBody User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getTel, user.getTel()).eq(User::getPwd, user.getPwd());
        User user1 = userService.getOne(lqw);
        boolean flag = user1 != null;
        if (flag) {
            String token = jwt.createToken(user1.getId().toString());
            user1.setToken(token);
        }
        return new Result(flag ? Code.GET_OK : Code.GET_ERR, user1, flag ? "登录成功！" : "用户名或密码错误！");
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        User user = userService.getById(id);
        boolean flag = user != null;
        return new Result(flag ? Code.GET_OK : Code.GET_ERR, user, flag ? "查询成功！" : "用户不存在！");
    }
}

